package practice;

import java.util.*;

/**
 * 最长的数学表达式
 */
public class LongestMathExpression {

    private static List<Character> opers = new ArrayList<Character>() {{
        add('+');
        add('-');
        add('*');
    }};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            String str = sc.nextLine();
            long result = calculateMaxExpression(str);
            System.out.println(result);
        }
    }

    private static long calculateMaxExpression(String input) {
        if (input == null || input.length() == 0) {
            return 0;
        }

        int maxLength = 0;
        long value = 0;
        int left = 0,right = 0;
        int n = input.length();
        while (right<n) {
            char c = input.charAt(right);
            if (!isValidCharacter(c)) {
                int length = right-left+1;
                String substr = input.substring(left, right);
                if (length > maxLength && isValidExpression(substr)) {
                    value = calculateExpression(substr);
                    maxLength = length;
                }
                left = right+1;
            }
            right++;
        }
        return value;
    }

    private static boolean isValidExpression(String expression) {
        for (int i = 1; i < expression.length(); i++) {
            char c1 = expression.charAt(i - 1);
            char c2 = expression.charAt(i);
            if (opers.contains(c1) && opers.contains(c2)) {
                return false;
            }
        }
        return true;
    }

    public static long calculateExpression(String expression) {
        Stack<Long> operandStack = new Stack<>();     // 存储操作数的栈
        Stack<Character> operatorStack = new Stack<>();     // 存储操作符的栈

        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);

            if (c == ' ') {
                continue;     // 如果是空格，则忽略该字符
            }

            if (Character.isDigit(c)) {
                // 遇到数字，将连续的数字字符组合成一个操作数，并将其压入操作数栈
                long num = 0;
                while (i < expression.length() && Character.isDigit(expression.charAt(i))) {
                    num = num * 10 + (expression.charAt(i) - '0');
                    i++;
                }
                operandStack.push(num);
                i--;
            } else if (c == '+' || c == '-' || c == '*') {
                // 遇到操作符
                while (!operatorStack.isEmpty() && hasHigherPrecedence(c, operatorStack.peek())) {
                    // 当操作符栈中存在操作符，并且当前操作符的优先级小于等于操作符栈顶的优先级时，需要先进行计算
                    evaluateTop(operandStack, operatorStack);
                }
                operatorStack.push(c);     // 将当前操作符压入操作符栈
            }
        }

        while (!operatorStack.isEmpty()) {
            // 处理剩余的操作符
            evaluateTop(operandStack, operatorStack);
        }

        return operandStack.pop();     // 返回最终的计算结果
    }

    public static boolean hasHigherPrecedence(char op1, char op2) {
        // 判断操作符op1的优先级是否高于操作符op2
        return (op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-');
    }

    public static void evaluateTop(Stack<Long> operandStack, Stack<Character> operatorStack) {
        // 执行操作符栈顶的运算
        char operator = operatorStack.pop();
        long operand2 = operandStack.pop();
        long operand1 = operandStack.pop();
        long result = 0;

        switch (operator) {
            case '+':
                result = operand1 + operand2;
                break;
            case '-':
                result = operand1 - operand2;
                break;
            case '*':
                result = operand1 * operand2;
                break;
        }

        operandStack.push(result);
    }


    private static boolean isValidCharacter(char c) {
        return opers.contains(c) || Character.isDigit(c);
    }
}
